package com.qianfeng.zhangwei.blutooth.taks;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

import java.io.IOException;
import java.util.UUID;

/**
 * @说 明：蓝牙连接服务器
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.blutooth.taks
 * @类 名： BluetoothServerThread
 * @创 建人： zhangwei
 * @创建时间：2015-08-11 10:51
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class BluetoothServerThread extends Thread {
    BluetoothServerSocket serverSocket;
    BluetoothAdapter mBluetoothAdapter;
    private BluetoothDevice bluetoothDevice;
    private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


    public BluetoothServerThread(BluetoothAdapter mBluetoothAdapter, BluetoothDevice bluetoothDevice) {
        this.mBluetoothAdapter = mBluetoothAdapter;
        this.bluetoothDevice = bluetoothDevice;
        try {
            serverSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(bluetoothDevice.getName(), uuid);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        super.run();
        BluetoothSocket socket = null;
        while (true) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
            // If a connection was accepted
            // Do work to manage the connection (in a separate thread)
            if (socket != null) {
                try {
                    manageConnectedSocket(socket);
                    serverSocket.close();
                    break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    private void manageConnectedSocket(BluetoothSocket socket) {
    }
}


